from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from qingmi_selenium import biz_mysql


def qingmi_selenium(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    driver.get("https://www.niuniutui.com/")
    driver.maximize_window()
    driver.implicitly_wait(20)
    driver.find_element_by_xpath('//a[text()="登录"]').click()
    # 定位账号
    driver.find_element_by_xpath('//*[@id="account"]').clear()
    driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
    # 定位密码
    driver.find_element_by_xpath('//*[@id="password"]').clear()
    driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
    # 验证码
    capt = input(">请输入验证码:")
    driver.find_element_by_xpath('//*[@id="captcha"]').send_keys(capt)
    # 点击登录
    driver.find_element_by_xpath('//*[@id="submit_btn"]').click()

    # 进入产品后台
    driver.find_element_by_xpath('//td[6]/a').click()

    # 小说列表
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//div/ul/li[9]/a'))
        )
        driver.find_element_by_xpath('//div/ul/li[9]/a').click()
    except Exception:
        print("点击小说列表")

    # 总付费榜
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//a[text()="总付费榜"]'))
        )
        driver.find_element_by_xpath('//a[text()="总付费榜"]').click()
    except Exception:
        print("点击总付费榜")

    # 输入关键字
    try:
        time.sleep(0.5)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//input[2]'))
        )
        driver.find_element_by_xpath('//input[2]').clear()
        driver.find_element_by_xpath('//input[2]').send_keys(Novel_name)
    except Exception:
        print("点击输入关键字")

    # 搜索
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//button'))
        )
        driver.find_element_by_xpath('//button').click()
    except Exception:
        print("点击搜索")

    # 获取内推链接
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//td[6]/a[2]'))
        )
        driver.find_element_by_xpath('//td[6]/a[2]').click()
    except Exception:
        print("点击获取内推链接")

    # 获取第一条内推链接
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="getlinkbtn1"]'))
        )
        driver.find_element_by_xpath('//*[@id="getlinkbtn1"]').click()
    except Exception:
        print("点击获取第一条内推链接")

    # 渠道名称
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="title"]'))
        )
        driver.find_element_by_xpath('//*[@id="title"]').clear()
        driver.find_element_by_xpath('//*[@id="title"]').send_keys(str(insert_time) + str(Extension_name))
    except Exception:
        print("点击渠道名称")

    # 生成链接
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="makelinkbtn"]'))
        )
        driver.find_element_by_xpath('//*[@id="makelinkbtn"]').click()
    except Exception:
        print("点击生成链接")

    # 获取url
    try:
        time.sleep(3)
        url = driver.execute_script("return jQuery('[id=linkurl]').val();")
        print(url)
        # 数据入库
        try:
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                         datetime=insert_time, Extension_name=str(insert_time) + str(Extension_name))
        except Exception as e:
            print(e)
            print("数据入库失败")
    except Exception:
        print("选择url框出错")

    # 取消
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//span[@class="layui-layer-setwin"]'))
        )
        driver.find_element_by_xpath('//span[@class="layui-layer-setwin"]').click()
    except Exception:
        print("点击取消")

    driver.quit()
